//
//  StarLevelViewController.m
//  LiTingFeng
//
//  Created by xinxuan on 16/1/26.
//  Copyright © 2016年 Bullgo. All rights reserved.
//

#import "StarLevelViewController.h"
#import "TrainViewController.h"
#import "CustomViewController.h"
#import "PlayerViewController.h"
@interface StarLevelViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *modelsArray;

@end

@implementation StarLevelViewController

static NSString *reuseID = @"cell1";


- (void)viewDidLoad {
    [super viewDidLoad];

    [self getModelWithCount:self.starNumber1 count:self.starNumber2];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    [self.view addSubview:self.tableView];
    
}





-(void)getModelWithCount:(int)count1 count:(int)count2{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"allData.plist" ofType:nil];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray * titleArray = [dic allKeys];
    NSMutableArray * dataArray = [NSMutableArray new];
    for (int i =0 ; i<titleArray.count ; i++) {
        NSArray *array = [dic objectForKey:titleArray[i]];
        [dataArray addObjectsFromArray:array];
    }
    
    _modelsArray = [NSMutableArray array];
    for (NSDictionary *dict in dataArray) {
        
        CommenModel *model = [CommenModel new];
        model.pic = dict[@"pic"];
        model.name = dict[@"name"];
        model.centent = dict[@"centent"];
        model.period = dict[@"period"];
        model.growth = dict[@"growth"];
        model.personNumber = dict[@"personNumber"];
        model.starCount = dict[@"starCount"];
        model.time = dict[@"time"];
        model.level = dict[@"level"];
        
        if ([dict[@"starCount"] integerValue] == count1 || [dict[@"starCount"] integerValue] == count2) {
            [self.modelsArray addObject:model];
        }
    }
    
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    
    cell.model = self.modelsArray[indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    CommenModel * model = [CommenModel new];
    model = _modelsArray[indexPath.row];
    
        NSString *string1 = model.name;
        NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 2)];
        if ([string2 isEqualToString:@"音乐"]) {
            PlayerViewController * playVC = [[PlayerViewController alloc]init];
            [self.navigationController pushViewController:playVC animated:YES];
        }else{
            TrainViewController * trainVc = [[TrainViewController alloc] init];
            trainVc.model = model;
            trainVc.nameString = model.name;
            trainVc.detailString = model.centent;
            trainVc.totalTime = [model.time integerValue] * 60;
            [self.navigationController pushViewController:trainVc animated:YES];
        }
    
}



- (void)setupStarImages
{
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (Screen_Width == 320 && Screen_Height == 480) {//苹果4
        
        return 130;
        
    }else if(Screen_Width == 320 && Screen_Height == 568){//苹果5
        
        return 130;
        
        
    }else if(Screen_Width == 375 && Screen_Height == 667){//苹果6
        
        return 150;
        
        
    }else{//苹果6puls及其以上
        
        return 160;
        
    }
    
    
}


#pragma mark ---- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 50  - 20 )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        [_tableView registerNib:[UINib nibWithNibName:@"BGTableViewCell" bundle:nil] forCellReuseIdentifier:reuseID];
    }
    return _tableView;
}

@end
